package com.eqdd.yiqidian.ui.companyspace

import android.app.Activity
import android.content.Intent
import android.support.v7.widget.RecyclerView
import android.view.View
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimImageSelectBean
import com.eqdd.yiqidian.ui.SingleChooseActivity
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity
import com.eqdd.yiqidian.utils.HttpUtil
import com.eqdd.yiqidian.utils.ImageUtil
import com.eqdd.yiqidian.utils.PicUtil
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import kotlinx.android.synthetic.main.activity_recyclerview.*
import kotlinx.android.synthetic.main.list_item_23.*
import org.jetbrains.anko.startActivityForResult

/**
 *
 * Created by jhj on 18-7-23.
 */
class ProductIndexSelectActivity : SlimBaseMultiEditActivity() {
    override fun initBinding() {
        clearData()
        setContentView(R.layout.activity_recyclerview)
        initTopTitleBar(View.VISIBLE, "指标")
        initTopRightText("确定") {
            val bean = slimAdapterEx.data
            val str = """{
                "indexTypeKey":"${bean[0].content}",
                "indexTypeValue":"${bean[1].content}",
                "indexImage":"${if (bean[2].tag as String? == null) "" else bean[2].tag as String?}"
                }""".trimMargin()
            val intent = Intent()
            intent.putExtra("data", str)
            setResult(Activity.RESULT_OK, intent)
            finish()
        }
    }

    override fun initRecyclerView(): RecyclerView {
        return recyclerView
    }

    override fun itemClick(p: Int) {
        when (p) {
            0 -> startActivityForResult<SingleChooseActivity>(Config.ADD, Config.TYPE to 49, Config.TITLE to "指标")
        }
    }

    override fun setEditBeans() {
        datas.add(SlimEditEnterBean("指     标", "", 0, "请选择").setDeal(true).setNeed(true))
        datas.add(SlimEditEnterBean("指\t标\t值", "", 1, "请输入").setNeed(true))
        datas.add(SlimImageSelectBean("指标图片", 2, ""))
        slimAdapterEx.register(R.layout.layout_image_selected, SlimInjector<SlimImageSelectBean> { data, injector ->
            injector?.text(R.id.tv_label, data?.title)
                    ?.clicked(R.id.iv_add) {
                        PicUtil.single(this@ProductIndexSelectActivity, true)
                    }
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK && requestCode == Config.ADD) {
            val str = data?.getStringExtra("data")
            slimAdapterEx.getDataItem(0).content = str
            slimAdapterEx.notifyItemChanged(0)
        }
        if (resultCode == RESULT_OK && requestCode == PictureConfig.CHOOSE_REQUEST) {
            val localMedias = PictureSelector.obtainMultipleResult(data)
            ImageUtil.setImage(localMedias?.get(0)?.compressPath, iv_add)
            HttpUtil.upLoadPic(this@ProductIndexSelectActivity, localMedias[0].compressPath) { url ->
                slimAdapterEx.getDataItem(2).tag = url
            }

        }

    }
}